package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 佟媛媛 on 18/10/23.
 */

@Component("I1003")
@Scope("prototype")
public class I1003 extends BaseComponent {

    private static Logger log = LoggerFactory.getLogger(I1003.class);

    /**
     * @auth 佟媛媛
     * @date 18/10/30 下午1:40
     * @desc
     * "bjjd.sgyyrq"
     * 簿记建档安排：提示位置，人工核对
     * 该表述为一般表述，非固定模式。判断申购要约的提交时间是否晚于发行日。若晚于发行日，提示簿记建档安排错误。
     * "1、检查申购要约日期关键词，不存在人工提示；(I1003)
     */

    @Override
    public List<ComponentResult> handlerM() {
        return common("M-3");
    }

    private  List<ComponentResult> common(String catalogId){
        List<ComponentResult> list = new ArrayList<>();
        setCatalogId(catalogId);
        KVText SGYYRQ = getKVTextValue(getCatalogId(),"JZBJJD","SGYYRQ");
        list.add(getComponentResult(SGYYRQ,null,"判断申购要约的提交时间是否晚于发行日","簿记建档安排","缺失"));
        return list;
    }

    private ComponentResult getComponentResult(KVText SGYYRQ,String text,String keyword,String ruleName,String promptMsg){
        String exist = "02";
        int pageNum = getChapterModel(getCatalogId()).getStartPage();
        List<ComponentResult.TextPosition> textPositionList = null;
        boolean annotationFlag = true;
        try{
            if(SGYYRQ!=null && StringUtils.isNotEmpty(SGYYRQ.getText())){
                pageNum = SGYYRQ.getPageNumber();
                textPositionList = getContentAndPositionList(SGYYRQ);
                KVText FXRQ = getKVTextValue(getCatalogId(),"FXTK","FXRQ");
                if(FXRQ!=null && StringUtils.isNotEmpty(FXRQ.getText())){
                    String FXRQS = FXRQ.getText();//发行日
                    String SGYYRQS = SGYYRQ.getText();//申购要约日期
                    Date FXRQD = null;
                    Date SGYYRQD = null;
                    // 判断申购要约的提交时间是否晚于发行日
                    String _FXRQS = getDate(FXRQS);
                    if(StringUtils.isNotEmpty(_FXRQS)){
                        FXRQS = _FXRQS.replace("年","-").replace("月","-").replace("日","");
                        FXRQD = DateUtils.parseDate(FXRQS,"yyyy-MM-dd");
                    }
                    String _SGYYRQS = getDate(SGYYRQS);
                    if(StringUtils.isNotEmpty(_SGYYRQS)){
                        SGYYRQS = _SGYYRQS.replace("年","-").replace("月","-").replace("日","");
                        SGYYRQD = DateUtils.parseDate(SGYYRQS,"yyyy-MM-dd");
                    }
                    if(FXRQD!=null && SGYYRQD!=null){
                        exist = "01";
                        annotationFlag = false;
                        double d = SGYYRQD.getTime() - FXRQD.getTime();
                        if(d>=0){
                            return getComponentResult(annotationFlag,exist,keyword,pageNum,"发行日期："+FXRQS+"，申购要约日期："+SGYYRQS,ruleName,textPositionList);
                        }else{
                            return getComponentResult(annotationFlag,exist,keyword,pageNum,"簿记建档安排错误",ruleName,textPositionList);
                        }
                    }
                    return getComponentResult(annotationFlag,exist,keyword,pageNum,promptMsg,ruleName,textPositionList);
                }else{
                    return getComponentResult(annotationFlag,exist,keyword,pageNum,"发行日不存在",ruleName,textPositionList);
                }
            }else{
                return getComponentResult(annotationFlag,exist,keyword,pageNum,"申购要约日期不存在",ruleName,textPositionList);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return getComponentResult(annotationFlag,exist,keyword,pageNum,promptMsg,ruleName,textPositionList);
        }
    }

    /**
     * @auth 佟媛媛
     * @date 18/12/3 上午10:41
     * @desc 判断是否日期格式
     */
    private String getDate(String date){
        date = date.replace(" ","");
        Pattern pattern = Pattern.compile("\\d{4}[年|-]【{0,1}\\d{1,2}】{0,1}[月|-]【{0,1}\\d{1,2}】{0,1}[日|号]{0,1}");
        Matcher matcher = pattern.matcher(date);
        if(matcher.find()){
            return matcher.group();
        }
        return null;
    }

    private ComponentResult getComponentResult(boolean annotationFlag,String exist,String keyword,int pageNum,String promptMsg,String ruleName,List<ComponentResult.TextPosition> textPositionList){
        ComponentResult result = new ComponentResult();
        result.setAnnotationFlag(annotationFlag);
        result.setCatalogId(getCatalogId());
        result.setChapterId(getChapterId());
        result.setExist(exist);
        result.setKeyword(keyword);
        result.setPageNum(pageNum);
        result.setPromptMessage(promptMsg);
        result.setRegulationId(getRegulationId());
        result.setRuleName(ruleName);
        result.setTextPosition(textPositionList);
        result.setUid(UUID.randomUUID().toString());
        return result;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        return common("BM-3");
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return common("DM-3");
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return common("BDM-3");
    }
}
